package com.ktg.mes.unit.other;

import com.ktg.common.utils.StringUtils;
import com.ktg.mes.md.service.impl.MdProductStatisticsBomServiceImpl;
import com.ktg.mes.pro.domain.ProTask;
import com.ktg.mes.pro.service.impl.ProItemNeedServiceImpl;
import com.ktg.mes.pro.service.impl.ProTaskServiceImpl;
import com.ktg.mes.wm.domain.WmStockWillOut;
import com.ktg.mes.wm.service.impl.WmItemRecptLineServiceImpl;
import com.ktg.mes.wm.service.impl.WmMaterialStockServiceImpl;
import com.ktg.mes.wm.service.impl.WmStockWillOutServiceImpl;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Slf4j
public class SQLListener {


    private static HashMap<String,List<SQLListenerModel>> ListenerMap = new HashMap<>();


    public static void findListener(){
        //获取表对应的Impl 的关联关联
        List<Class> classList = new ArrayList<>();
        classList.add(WmItemRecptLineServiceImpl.class);
        classList.add(WmMaterialStockServiceImpl.class);
        classList.add(WmStockWillOutServiceImpl.class);
        classList.add(ProTaskServiceImpl.class);
        classList.add(ProItemNeedServiceImpl.class);
        classList.add(MdProductStatisticsBomServiceImpl.class);
        SQLListener.findImplListener(classList);
    }

    /**
     * 根据表名导入
     * @param impls
     */
    public static void findImplListener(List<Class> impls){
        if (impls == null || impls.size() == 0 || impls.get(0) == null){
            return;
        }
        HashMap<String,List<SQLListenerModel>> hashMap = new HashMap<>();
        try {
            for (int z = 0; z < impls.size(); z++) {
                Class tClass = impls.get(z);
                Class aClass = impls.get(z);
                if (tClass != null){
                    List<Method> methodList = new ArrayList<>();
                    int subCount = 0;
                    while (tClass != null && subCount < 3) {//当父类为null的时候说明到达了最上层的父类(Object类).
                        methodList.addAll(Arrays.asList(tClass.getDeclaredMethods()));
                        tClass = tClass.getSuperclass(); //得到父类,然后赋给自己
                        subCount++;
                    }
                    for (int i = 0;i < methodList.size();i++){
                        Method method = methodList.get(i);
                        SQLListenerInterface SQLListenerInterface = method.getAnnotation(SQLListenerInterface.class);

                        String tableFieldName = "";
                        if (SQLListenerInterface != null && SQLListenerInterface.tableName() != null && SQLListenerInterface.tableName().trim().length() > 0){
                            tableFieldName = SQLListenerInterface.tableName();
                            SQLListenerType[] types = SQLListenerInterface.listerType();

                            if (types != null && !StringUtils.isEmpty(tableFieldName)){
                                for (int j = 0; j < types.length; j++) {
                                    SQLListenerType type = types[j];

                                    if (type == SQLListenerType.BeforeAll){
                                        addToMap(hashMap,tableFieldName,SQLListenerType.AddBefore,method,aClass);
                                        addToMap(hashMap,tableFieldName,SQLListenerType.ChangeBefore,method,aClass);
                                        addToMap(hashMap,tableFieldName,SQLListenerType.DeleteBefore,method,aClass);
                                    }else if (type == SQLListenerType.AfterAll){
                                        addToMap(hashMap,tableFieldName,SQLListenerType.AddAfter,method,aClass);
                                        addToMap(hashMap,tableFieldName,SQLListenerType.ChangeAfter,method,aClass);
                                        addToMap(hashMap,tableFieldName,SQLListenerType.DeleteAfter,method,aClass);
                                    }else {
                                        addToMap(hashMap,tableFieldName,type,method,aClass);
                                    }

                                }
                            }
                        }
                    }

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        ListenerMap = hashMap;
    }


    public static void addToMap(HashMap<String,List<SQLListenerModel>> hashMap,String tableFieldName,SQLListenerType type,Method method,Class aClass){
        List<SQLListenerModel> list = hashMap.get(tableFieldName+":"+ type.toString());
        if (list == null){
            list = new ArrayList<>();
        }
        SQLListenerModel SQLListenerModel = new SQLListenerModel();
        SQLListenerModel.setMethod(method);
        SQLListenerModel.setClassName(aClass);
        list.add(SQLListenerModel);
        hashMap.put(tableFieldName+":"+type.toString(),list);
    }

    public static List<SQLListenerModel>  listenerImpl(String tableNameAction){
        if (ListenerMap != null){
            return ListenerMap.get(tableNameAction);
        }
        return null;
    }

    public static HashMap<String,List<SQLListenerModel>> getListenerMap(){
        return ListenerMap;
    }




    /**
     * 将下划线转化成驼峰命名
     * @param para
     * @return
     */
    public static String camelToSuperline(String para){
        if(para.length()<3){
            return para.toUpperCase();
        }
        String[] list = para.split("_");
        String str = list[0].substring(0,1).toUpperCase()+list[0].substring(1,list[0].length());
        for (int i = 1;i < list.length;i++){
            String strItem = list[i];
            str += strItem.substring(0,1).toUpperCase()+strItem.substring(1,strItem.length());
        }
        return str;
    }


}
